﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitiesDAL
{
    public class DuocMuonDAL : BaseDAL, UTL.IBaseDAL 
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Duocmuon");
        }

        public DataTable Select_sachmuon()
        {
            return ExecuteQuery("sp_SelectAll_Sachmuon");
        }


        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Duocmuon {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (DuocMuon)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Duocmuon '{0}', '{1}' ";
            sql = string.Format(sql, o.STT_PM, o.MACUONSACH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (DuocMuon)obj;
            var sql = "set dateformat dmy; execute sp_Update_Duocmuon '{0}', '{1}' ";
            sql = string.Format(sql, o.STT_PM, o.MACUONSACH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Update_CuonSach(string stt_pm, string macuonsach, string macuonsach_stt)
        {
            var sql = @"set dateformat dmy; execute sp_Update_Duocmuon_CuonSach '" + stt_pm + "', '" + macuonsach + "', '" + macuonsach_stt + "' ";
            sql = string.Format(sql, stt_pm, macuonsach, macuonsach_stt);
            return ExecuteQuery(sql);
        }

        public DataTable Search(string name)
        {
            var sql = @"Select c.STT_PM, b.MACUONSACH, d.TENDAUSACH, d.NOIDUNG_TT, d.LANXUATBAN, d.KICHTHUOC, d.SOTRANG, e.TENKESACH, f.TENTHELOAI, i.TENNHAXB, c.NGAYMUON
                                from tb_Duocmuon a join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH join tb_Phieumuon c on a.STT_PM=c.STT_PM join tb_Dausach d on b.MADAUSACH=d.MADAUSACH join tb_Kesach e on b.MAKESACH=e.MAKESACH join tb_Theloai f on d.MATHELOAI=f.MATHELOAI join tb_Nhaxuatban i on d.MANHAXB=i.MANHAXB";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search_FromDate_ToDate(DateTime FromDate, DateTime ToDate)
        {
            var sql = @"set dateformat dmy; execute sp_SelectAll_Sachmuon_FromDate_ToDate '" + FromDate.ToString("dd/MM/yyyy") + "' , '" + ToDate.ToString("dd/MM/yyyy") + "' ";
            sql = string.Format(sql, FromDate);
            return ExecuteQuery(sql);
        }

        public DataTable Search_Month(int month)
        {
            var sql = @"set dateformat dmy; execute sp_SelectAll_Sachmuon_Month '" + month + "' ";
            sql = string.Format(sql, month);
            return ExecuteQuery(sql);
        }
        public DataTable Search_Nam(string nam)
        {
            var sql = @"set dateformat dmy; execute sp_SelectAll_Sachmuon_Nam '" + nam + "' ";
            sql = string.Format(sql, nam);
            return ExecuteQuery(sql);
        }
        public DataTable Search_Quater(string quater)
        {
            var sql = @"set dateformat dmy; execute sp_SelectAll_Sachmuon_Quater N'" + quater + "' ";
            sql = string.Format(sql, quater);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.DuocMuon o)
        {
            string sql;
            if (o.STT_PM != "")
                sql = @"Select c.STT_PM, b.MACUONSACH, d.TENDAUSACH, d.NOIDUNG_TT, d.LANXUATBAN, d.KICHTHUOC, d.SOTRANG, e.TENKESACH, f.TENTHELOAI, i.TENNHAXB, c.NGAYMUON
                        from tb_Duocmuon a join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH join tb_Phieumuon c on a.STT_PM=c.STT_PM join tb_Dausach d on b.MADAUSACH=d.MADAUSACH join tb_Kesach e on b.MAKESACH=e.MAKESACH join tb_Theloai f on d.MATHELOAI=f.MATHELOAI join tb_Nhaxuatban i on d.MANHAXB=i.MANHAXB Where c.STT_PM like '%" + o.STT_PM + "%'  ";
            else
                sql = @"Select c.STT_PM, b.MACUONSACH, d.TENDAUSACH, d.NOIDUNG_TT, d.LANXUATBAN, d.KICHTHUOC, d.SOTRANG, e.TENKESACH, f.TENTHELOAI, i.TENNHAXB, c.NGAYMUON
                             from tb_Duocmuon a join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH join tb_Phieumuon c on a.STT_PM=c.STT_PM join tb_Dausach d on b.MADAUSACH=d.MADAUSACH join tb_Kesach e on b.MAKESACH=e.MAKESACH join tb_Theloai f on d.MATHELOAI=f.MATHELOAI join tb_Nhaxuatban i on d.MANHAXB=i.MANHAXB Where b.MACUONSACH like '%" + o.MACUONSACH + "%' ";
            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(STT_PM) as Ma From tb_Duocmuon";
            return ExecuteQuery(sql);
        }


        #endregion
    }
}
